using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;

public partial class admin_webparts_Plug_ins_Files_CreateDir : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string _Message = string.Empty;
    public string _CurrentPath = "/";

    protected void Page_Load(object sender, EventArgs e)
    {
        btnSave1.Text = LanguageUtility.GetLanguageText("CREATE_DIR", Session);

        try
        {
            string action = Request.QueryString["action"];

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }
            
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            if (!Directory.Exists(currentServerPath))
            {
                Response.Redirect("?type=page&name=files", false);
                return;
            }
        }
        catch (Exception ex)
        {
            _Message = ex.Message;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            string folderName = txtName.Text.Trim().Trim('/');
            string folderPath = Path.Combine(currentServerPath, folderName);
            if (Directory.Exists(folderPath))
            {
                _Message = LanguageUtility.GetLanguageText("EXISTING_FOLDER_ERR", Session);
                return;
            }

            Directory.CreateDirectory(folderPath);
            this.SetCookie(FILES_MESSAGE_TYPE, "0");
            this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("DIR_CREATED", Session));
            Response.Redirect("?type=page&name=files&file=" + Server.UrlEncode(this._CurrentPath), false);
            return;
        }
        catch (Exception ex)
        {
            _Message = ex.Message;
        }
    }

    public void SetCookie(string name, string value)
    {
        //CookieUtility.SetCookie(name, value, DateTime.Now.AddDays(1), Request, Response);
        Session[name] = value;
    }
}
